﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manager_Sanpham1_Controls_UC_Themsanpham : System.Web.UI.UserControl
{
    BUS_Product data = new BUS_Product();
    BUS_Manufacturer data2 = new BUS_Manufacturer();
    DTO_Product obj = new DTO_Product();
    BUS_GroupProduct group = new BUS_GroupProduct();
    protected void Page_Load(object sender, EventArgs e)
    {
        hplXemloaiSP.NavigateUrl = "../Loaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
        hplThemsanpham.NavigateUrl = "../Themsanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
        hplSualoiSP.NavigateUrl = "../Sualoaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
        if (!IsPostBack)
        {
        CKEditorControl1.Width = 780;
        CKEditorControl1.Skin = "kama";
        CKEditorControl1.Language = "vi";
        CKEditorControl1.RemovePlugins = "a11yhelp";
        CKEditorControl1.RemovePlugins = "find";
        DropDownList1.DataSource = data2.Manufacturer_GetByAll();
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataBind();
        DropDownList1.SelectedValue = Request.QueryString["id_nsx"];
        string id_nsx = Request.QueryString["id_nsx"];
        dlLoaiSP.DataSource = group.GroupProduct_GetByAll();
        dlLoaiSP.DataBind();
        }
    }
    protected void btnThemloaisanpham_Click(object sender, EventArgs e)
    {
        int idd = int.Parse(DropDownList1.SelectedValue);
        obj.Manufacturer_Id = idd;
        obj.Name = txtTensanpham.Text;
        obj.Detail = CKEditorControl1.Text;
        string a = txtGia.Text.ToString();
        obj.Price = a;
        obj.GroupProduct_Id = Group();
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/Upload/Product/" + FileUpload1.FileName));
            obj.Image = "~/Upload/Product/" + FileUpload1.FileName;
        }
        else
        {
            obj.Image = txtUrlImage.Text;
        }
        
        obj.Date = string.Format("{0:G}", DateTime.Now);
        data.Product_Insert(obj);
        //Alert.Text = Thongbao("Đã thêm thành công");
        Response.Redirect("../Sanpham/Loaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"]);

    }
    public string Thongbao(string text)
    {
        return "<script>alert('" + text + "')</script>";
    }
    public void UploadAnh()
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/Images/product/" + FileUpload1.FileName));
        }
    }

    public string Group()
    {
        string li = "";
        foreach (DataListItem item in dlLoaiSP.Items)
        {

            CheckBox linh = (CheckBox)item.FindControl("CheckBox1");
            if (linh.Checked)
            {
                TextBox tb = (TextBox)item.FindControl("TextBox1");
                li += tb.Text + "|";
            }
        }
        return li;
    }

}